#include "AvatarArea.h"
#include "Icons.h"
#include <wx/dcbuffer.h>
#include "../../core/Impp.h"

namespace Impp
{

BEGIN_EVENT_TABLE(AvatarArea, wxPanel)
	EVT_PAINT(AvatarArea::onPaint)
	EVT_ENTER_WINDOW(AvatarArea::onMouseEntered)
	EVT_LEAVE_WINDOW(AvatarArea::onMouseLeaved)
	EVT_ERASE_BACKGROUND(AvatarArea::OnEraseBackground)
END_EVENT_TABLE()

AvatarArea::AvatarArea(wxPanel* parent, long id, Buddy* owner,const wxSize & size) :
	wxPanel(parent, id, wxDefaultPosition, wxDefaultSize/*, wxFULL_REPAINT_ON_RESIZE|wxTE_MULTILINE*/),
	owner(owner),size(size)
{
	avatarChanged();
	SetSize(size);
	SetMaxSize(size);
	SetMinSize(size);

	toolTip = new EntryToolTip(this);
}

AvatarArea::~AvatarArea()
{
}

void AvatarArea::avatarChanged(){
	avatar = owner->getAvatar();
	if(avatar.IsNull()){
		iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
		if(ui != NULL){
			avatar = ui->getIcons()->getSpecialBitmap(BITMAP_NO_AVATAR);
		}
	}
	if(!avatar.IsNull()){
		wxImage image = avatar.ConvertToImage();
		avatarScaled = wxBitmap(image.Scale(size.x,size.y,wxIMAGE_QUALITY_HIGH));
		//avatarScaled = avatar->Rescale(0,0,avatar->GetWidth(), avatar->GetHeight(),size.x,size.y);
		Refresh(true);
	}
}

void AvatarArea::onPaint(wxPaintEvent& evt){
    wxBufferedPaintDC dc(this);
    if(!avatarScaled.IsNull()){
    	 dc.DrawBitmap(avatarScaled,wxPoint(0,0),false);
    }
}

void AvatarArea::onMouseEntered(wxMouseEvent & event){
	wxPoint pos = GetScreenPosition();
	toolTip->trigger(owner,pos+size);
}
void AvatarArea::onMouseLeaved(wxMouseEvent & event){
	toolTip->hide();
}

void AvatarArea::OnEraseBackground(wxEraseEvent& evt){

}

}
